package cn.hftoday.core;

import java.util.*;
import junit.framework.TestCase;
import cn.hftoday.db.*;
import cn.hftoday.org.Account;
import cn.hftoday.util.*;

public class ConversationTest extends TestCase {

	protected void setUp() throws Exception {
		ApplicationConfig.getInstance().load();
		ConnectionPool.getInstance().open();
		ConnectionPool.getInstance().beginTransaction();
	}
	
	protected void tearDown() throws Exception {
		ConnectionPool.getInstance().rollback();
		ConnectionPool.getInstance().close();
	}

	public void testGetId() {
		Conversation con = new Conversation();
		con.setId("abc");
		assertEquals(con.getId(), "abc");
	}

	public void testGetCreateTime() {
		Conversation con = new Conversation();
		Calendar ca = Calendar.getInstance();
		con.setCreateTime(ca);
		assertEquals(con.getCreateTime(), ca);
	}

	public void testGetUpdateTime() {
		Conversation con = new Conversation();
		Calendar ca = Calendar.getInstance();
		con.setUpdateTime(ca);
		assertEquals(con.getUpdateTime(), ca);
	}

	public void testGetCode() {
		Conversation con = new Conversation();
		con.setCode(101);
		assertEquals(con.getCode(), 101);
	}
	
	public void testGetAccount() {
		//insert a account instance for test
		Account acct = new Account();
		acct.setId("23345678-1234-5678-12345678-12345678");
		acct.setName("luhaitao");
		acct.setEmail("luhaitao@hftoday.cn");
		acct.setPassword("123456");
		acct.setCreateTime(CalendarParser.toCalendar("2009-01-12 21:34:50"));
		acct.setPerson(null);
		acct.setCompany(null);
		Mapper.getInstance().insert("InsertAccount", acct);

		//test conversation
		Conversation con = new Conversation();
		con.setAccount(acct);
		
		Account a = con.getAccount();
		assertEquals(a.getId(), "23345678-1234-5678-12345678-12345678"); 
	}
	
	public void testSelect() {
		testInsert();
		Conversation c = (Conversation)Mapper.getInstance().queryForObject("SelectConversationById", "abcdefghijklmn");
		
		assertEquals(c.getCode(), 70001);
		assertEquals(c.getCreateTime(), CalendarParser.toCalendar("2009-01-11 12:21:00"));
		assertEquals(c.getUpdateTime(), CalendarParser.toCalendar("2009-01-11 12:22:00"));
		assertEquals(c.isRemember(), true);
	}
	
	public void testInsert() {
		Conversation c = new Conversation();
		c.setCode(70001);
		c.setCreateTime(CalendarParser.toCalendar("2009-01-11 12:21:00"));
		c.setUpdateTime(CalendarParser.toCalendar("2009-01-11 12:22:00"));
		c.setId("abcdefghijklmn");
		c.setRemember(true);
		Object o = Mapper.getInstance().insert("InsertConversation", c);
		assertSame(c, o);
	}
	
	public void testUpdate() {
		testInsert();
		Conversation c = (Conversation)Mapper.getInstance().queryForObject("SelectConversationById", "abcdefghijklmn");
		c.setCode(70002);
		c.setUpdateTime(CalendarParser.toCalendar("2009-01-11 12:23:00"));
		c.setRemember(false);
		Mapper.getInstance().update("UpdateConversation", c);
		c = (Conversation)Mapper.getInstance().queryForObject("SelectConversationById", "abcdefghijklmn");
		
		assertEquals(c.getCode(), 70002);
		assertEquals(c.getCreateTime(), CalendarParser.toCalendar("2009-01-11 12:21:00"));
		assertEquals(c.getUpdateTime(), CalendarParser.toCalendar("2009-01-11 12:23:00"));
		assertEquals(c.isRemember(), false);
	}
	
	public void testDelete() {
		testInsert();
		Conversation c = (Conversation)Mapper.getInstance().queryForObject("SelectConversationById", "abcdefghijklmn");
		Mapper.getInstance().delete("DeleteConversation", c);
		List list = Mapper.getInstance().queryForList("SelectConversation");
		assertEquals(list.size(), 0); 
	}
}
